/*
 * Created by JFormDesigner on Sat Mar 26 21:53:27 CST 2022
 */

package com.gxu.sjjg;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;

/**
 * @author unknown
 */
public class AddGrade extends JFrame {
    public AddGrade() {//类构造方法，调用界面方法
        initComponents();
    }


    //添加学生数据，添加完后不退出界面，可以连续添加。
    private void ok(ActionEvent ee)  {
        boolean error=false;
        String regex="[1-9]\\d{1,2}",regex2="\\d{1,9}";//使用正则表达式
        String match1="[\\u4e00-\\u9fa5]{1,6}";//汉字的unicode编码范围为\u4e00-\u9fa5,
        //判断输入数据的格式是否有问题
        if(!textField1.getText().matches(match1)||!(textField2.getText().matches(regex2)))//姓名和学号需符合格式
            error=true;
        else if(!(textField3.getText().matches(regex)&&Integer.parseInt(textField3.getText().trim())<=100)||!(textField4.getText().matches(regex)&&Integer.parseInt(textField4.getText().trim())<=100)||!(textField5.getText().matches(regex)&&Integer.parseInt(textField5.getText().trim())<=100)||!(textField6.getText().matches(regex)&&Integer.parseInt(textField6.getText().trim())<=100))
            error=true;
        if(error){//格式不正确，提示数据非法
            JOptionPane.showMessageDialog(null, "数据不合法！", "提示", JOptionPane.WARNING_MESSAGE);
        }
        else {//格式正确
            try {
                BufferedReader fbr = new BufferedReader(new FileReader(new File("src/file/Grade.txt")));
                fbr.readLine();//先把第一行读完
                String s;
                boolean exsist = false;
                while ((s = fbr.readLine()) != null) {//查询输入的数据中的学号是否已经存在，即是否该学生的信息已经添加！
                    int i = 0;
                    if (s.substring(i + 10, i + 19).trim().equals(textField2.getText()))
                        exsist = true;
                }
                fbr.close();
                if (!exsist) {//如果不存在，就添加进文件。
                    BufferedWriter inbf = new BufferedWriter(new FileWriter(new File("src/file/Grade.txt"), true));//传参数true，这样将以追加的方式写入学生信息
                    int len = 10 - textField1.getText().length();//len为补空格的数量，每条数据的属性占10个字符，不足补空格。
                    inbf.write(textField1.getText());
                    for (int i = 0; i < len; i++)
                        inbf.write(" ");
                    len = 10 - textField2.getText().length();
                    inbf.write(textField2.getText());
                    for (int i = 0; i < len; i++)
                        inbf.write(" ");
                    len = 10 - textField3.getText().length();
                    inbf.write(textField3.getText());
                    for (int i = 0; i < len; i++)
                        inbf.write(" ");
                    len = 10 - textField4.getText().length();
                    inbf.write(textField4.getText());
                    for (int i = 0; i < len; i++)
                        inbf.write(" ");
                    len = 10 - textField5.getText().length();
                    inbf.write(textField5.getText());
                    for (int i = 0; i < len; i++)
                        inbf.write(" ");
                    len = 10 - textField6.getText().length();
                    inbf.write(textField6.getText());
                    for (int i = 0; i < len; i++)
                        inbf.write(" ");
                    inbf.newLine();
                    inbf.close();
                    JOptionPane.showMessageDialog(null, "添加成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
                } else//如果已经存在，提示已存在。
                    JOptionPane.showMessageDialog(null, "已存在，请勿重复添加！", "提示", JOptionPane.WARNING_MESSAGE);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    private void button1(ActionEvent e) {//退出按钮
        dispose();
    }

    //界面代码
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        textField1 = new JTextField();
        label1 = new JLabel();
        textField2 = new JTextField();
        label2 = new JLabel();
        textField3 = new JTextField();
        label3 = new JLabel();
        textField4 = new JTextField();
        label4 = new JLabel();
        textField5 = new JTextField();
        label5 = new JLabel();
        label6 = new JLabel();
        ok = new JButton();
        button1 = new JButton();
        textField6 = new JTextField();
        bgp = new JLabel();

        //======== this ========
        setVisible(true);
        setResizable(false);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setLayout(null);
            panel1.add(textField1);
            textField1.setBounds(160, 35, 115, 30);

            //---- label1 ----
            label1.setText("\u59d3\u540d\uff1a");
            label1.setForeground(Color.black);
            panel1.add(label1);
            label1.setBounds(115, 40, 40, 25);
            panel1.add(textField2);
            textField2.setBounds(160, 75, 115, 30);

            //---- label2 ----
            label2.setText("\u5b66\u53f7\uff1a");
            label2.setForeground(Color.black);
            panel1.add(label2);
            label2.setBounds(115, 80, 40, 25);
            panel1.add(textField3);
            textField3.setBounds(160, 110, 115, 30);

            //---- label3 ----
            label3.setText("\u8bed\u6587\uff1a");
            label3.setForeground(Color.black);
            panel1.add(label3);
            label3.setBounds(115, 115, 40, 25);
            panel1.add(textField4);
            textField4.setBounds(160, 145, 115, 30);

            //---- label4 ----
            label4.setText("\u6570\u5b66\uff1a");
            label4.setForeground(Color.black);
            panel1.add(label4);
            label4.setBounds(115, 150, 40, 25);
            panel1.add(textField5);
            textField5.setBounds(160, 180, 115, 30);

            //---- label5 ----
            label5.setText("\u82f1\u8bed\uff1a");
            label5.setForeground(Color.black);
            panel1.add(label5);
            label5.setBounds(115, 185, 40, 25);

            //---- label6 ----
            label6.setText("\u4f53\u80b2\uff1a");
            label6.setForeground(Color.black);
            panel1.add(label6);
            label6.setBounds(115, 220, 40, 25);

            //---- ok ----
            ok.setText("\u6dfb\u52a0");
            ok.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
            ok.setContentAreaFilled(false);
            ok.addActionListener(e -> {
			ok(e);
		});
            panel1.add(ok);
            ok.setBounds(115, 270, 70, 25);

            //---- button1 ----
            button1.setText("\u8fd4\u56de");
            button1.setContentAreaFilled(false);
            button1.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
            button1.addActionListener(e -> button1(e));
            panel1.add(button1);
            button1.setBounds(215, 270, 70, 25);
            panel1.add(textField6);
            textField6.setBounds(160, 220, 115, 30);

            //---- bgp ----
            bgp.setHorizontalAlignment(SwingConstants.CENTER);
            bgp.setIcon(new ImageIcon(getClass().getResource("/myimage/loginimg.jpg")));
            panel1.add(bgp);
            bgp.setBounds(0, -5, 390, 330);
        }
        contentPane.add(panel1);
        panel1.setBounds(0, 0, 390, 325);

        contentPane.setPreferredSize(new Dimension(390, 355));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }


    private JPanel panel1;
    private JTextField textField1;
    private JLabel label1;
    private JTextField textField2;
    private JLabel label2;
    private JTextField textField3;
    private JLabel label3;
    private JTextField textField4;
    private JLabel label4;
    private JTextField textField5;
    private JLabel label5;
    private JLabel label6;
    private JButton ok;
    private JButton button1;
    private JTextField textField6;
    private JLabel bgp;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
